//
//  NewsViewController.m
//  网易新闻段子
//
//  Created by 洛洛大人 on 16/6/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewsViewController.h"
#import "ImageModel.h"
#import "WangyiTableViewCell.h"
#import "GetHeightTools.h"
#import "ImageTableViewCell.h"


@interface NewsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *wangyiOfArray;
@property(nonatomic, retain)UITableView *tableView;

@end

@implementation NewsViewController


-(void)dealloc {
    [_wangyiOfArray release];
    [_tableView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handleData];
    [self creatTableView];
     
}

-(void)config{
    
    self.wangyiOfArray = [NSMutableArray array];
    
    self.navigationItem.title = @"網易";
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    

    self.automaticallyAdjustsScrollViewInsets = NO;
    

}

-(void)handleData {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@".txt"];
    
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dic objectForKey:@"段子"];
    
  
    for (NSDictionary *dic in array) {
        
        ImageModel *wangyi = [[ImageModel alloc]init];
        
        [wangyi setValuesForKeysWithDictionary:dic];
        
        
        [self.wangyiOfArray addObject:wangyi];
       

   }
    
    
   
}



-(void)creatTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.rowHeight = 80;
    
    [self.tableView registerClass:[WangyiTableViewCell class] forCellReuseIdentifier:@"textCell"];
    [self.tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"imageCell"];
    

    
 }



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ImageModel *model = [self.wangyiOfArray objectAtIndex:indexPath.row];
   
    if(model.img == nil) {
        WangyiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        cell.cellModel = model;
        return cell;
      
        
    } else {
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
        cell.modelOfImage = model;
        return cell;
   }
   
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.wangyiOfArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ImageModel *model = [self.wangyiOfArray objectAtIndex:indexPath.row];
    if(model.img == nil){
        
     CGFloat height = [GetHeightTools heightWith:model.digest];
      return height + 60;
    } else {
        
        CGFloat imgheight = [GetHeightTools imageHeightWith:model.img];
        return imgheight + 70;
        
        
    }
    
    
}






@end
